package com.xuecheng.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.comment.model.dto.CommentDTO;
import com.xuecheng.api.comment.model.vo.CommentVO;
import com.xuecheng.api.content.model.dto.CoursePubDTO;
import com.xuecheng.api.teaching.model.dto.CompanyDTO;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.enums.common.CommonEnum;
import com.xuecheng.common.enums.content.TeachPlanEnum;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.teaching.agent.CommentApiAgent;
import com.xuecheng.teaching.agent.CourseBaseApiAgent;
import com.xuecheng.teaching.common.constant.TeachingErrorCode;
import com.xuecheng.teaching.convert.CompanyConvert;
import com.xuecheng.teaching.entity.Company;
import com.xuecheng.teaching.mapper.CompanyMapper;
import com.xuecheng.teaching.service.CompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

@Slf4j
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService{

    @Autowired
    private CommentApiAgent commentApiAgent;

    @Autowired
    private CourseBaseApiAgent courseBaseApiAgent;


    @Override
    public CompanyDTO getByTenantId(Long tenantId) {
        if (tenantId == null || tenantId <= 0) {
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }
        LambdaQueryWrapper<Company> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Company::getTenantId, tenantId);
        Company company = this.getOne(queryWrapper);
        return CompanyConvert.INSTANCE.entity2dto(company);
    }
    /*

     */
    @Override
    public CommentDTO createComment(CommentVO vo, Long companyId, String username) {
        //1.判断关键数据
        //     评论内容，用户名是否存在
        //     	不存在：抛出异常
        //1.1判断是否登录
        if (ObjectUtils.isEmpty(companyId)){
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }
        //1.2判断前端传入的数据是否为空
        if (ObjectUtils.isEmpty(vo)){
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }
        //1.3用户评分0-5不能为空
        if (StringUtils.isEmpty(vo.getStarRank().toString())){
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }
        //1.3评论对象ID[课程发布ID]不能为空
        if (StringUtils.isEmpty(vo.getTargetId().toString())){
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }

        //数据库的必填验证  ---评论内容
        if (StringUtils.isEmpty(vo.getCommentText())){
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }
        //根据getTargetId获取课程信息
        CoursePubDTO coursePub = courseBaseApiAgent.getCourseBaseByIdNoCompanyId(new Long(vo.getTargetId()));

        System.out.println(coursePub);
        //判断课程是否存在
        if(ObjectUtils.isEmpty(coursePub)){
            ExceptionCast.cast(CommonErrorCode.E_100104);
        }
        //判断课程的状态
        String status = coursePub.getStatus().toString();
        //判断是否删除
        if (CommonEnum.DELETE_FLAG.getCodeInt().equals(status)||
        CommonEnum.TRANSIENT_FLAG.getCodeInt().equals(status))
        {
            ExceptionCast.cast(TeachingErrorCode.E_130204);
        }
        //判断是否发布
        if (TeachPlanEnum.UNPUBLISH_STATUS.getCode().equals(coursePub.getIsPub()))
        {
            ExceptionCast.cast(TeachingErrorCode.E_130204);
        }
        // TODO: 2021/12/26
        //2.2判断课程是否收费（非必须）
        //			收费：查询用户是否购买该课程，未购买不允许评论
        // TODO: 2021/12/26
        //			2.2.1根据用户信息和课程id查询订单是否已付款
        //					未付款，不允许评论

        //新增评论
        CommentDTO comment = commentApiAgent.createComment(vo, username);

        return comment;
    }
}
